package org.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dao.KcDao;
import org.dao.XsDao;
import org.dao.ZyDao;
import org.model.Dlb;
import org.model.Kcb;
import org.model.Xsb;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class XsAction extends ActionSupport {
	
	//新增xsDao 和 Dlb 对象 ，只在构造函数中使用，不做页面变量使用
	//因此不需要生成getter and setter
	private XsDao xsDao;
	private Dlb user;
	//新增一个action 变量，这个变量待会儿再后面的xsinfo.jsp页面中会使用到
	private Xsb xsInfoJsp_xs;
	private Xsb updateXsInfo_xs;
	private List updateXsInfoJsp_zys;
	private ZyDao zyDao;
	private Xsb updateXsInfoJsp_xs;
	private Xsb updateActionXs;
	private List allKcJsp_kcs;
	private KcDao kcDao;
	private KcDao dlDao;
	private Kcb selectActionKcb;//选择课程表
	private Kcb deleteActionKcb;//删除课程表
	private Set<Kcb> xsKcsJsp_kcs;//个人选课情况
	
	
	public ZyDao getZyDao() {
		return zyDao;
	}
	public void setZyDao(ZyDao zyDao) {
		this.zyDao = zyDao;
	}
	public KcDao getDlDao() {
		return dlDao;
	}
	public void setDlDao(KcDao dlDao) {
		this.dlDao = dlDao;
	}
	public XsDao getXsDao() {
		return xsDao;
	}
	public void setXsDao(XsDao xsDao) {
		this.xsDao = xsDao;
	}
	public Kcb getDeleteActionKcb() {
		return deleteActionKcb;
	}
	public void setDeleteActionKcb(Kcb deleteActionKcb) {
		this.deleteActionKcb = deleteActionKcb;
	}
	public Set<Kcb> getXsKcsJsp_kcs() {
		return xsKcsJsp_kcs;
	}
	public void setXsKcsJsp_kcs(Set<Kcb> xsKcsJsp_kcs) {
		this.xsKcsJsp_kcs = xsKcsJsp_kcs;
	}
	public Kcb getSelectActionKcb() {
		return selectActionKcb;
	}
	public void setSelectActionKcb(Kcb selectActionKcb) {
		this.selectActionKcb = selectActionKcb;
	}
	//get set method
	public KcDao getKcDao() {
		return kcDao;
	}
	public void setKcDao(KcDao kcDao) {
		this.kcDao = kcDao;
	}
	public List<?> getAllKcJsp_kcs() {
		return allKcJsp_kcs;
	}
	public void setAllKcJsp_kcs(List<?> allKcJsp_kcs) {
		this.allKcJsp_kcs = allKcJsp_kcs;
	}
	public Xsb getUpdateActionXs() {
		return updateActionXs;
	}
	public void setUpdateActionXs(Xsb updateActionXs) {
		this.updateActionXs = updateActionXs;
	}
	public Xsb getUpdateXsInfoJsp_xs() {
		return updateXsInfoJsp_xs;
	}
	public void setUpdateXsInfoJsp_xs(Xsb updateXsInfoJsp_xs) {
		this.updateXsInfoJsp_xs = updateXsInfoJsp_xs;
	}
	public List<?> getUpdateXsInfoJsp_zys() {
		return updateXsInfoJsp_zys;
	}
	public void setUpdateXsInfoJsp_zys(List<?> updateXsInfoJsp_zys) {
		this.updateXsInfoJsp_zys = updateXsInfoJsp_zys;
	}
	public Xsb getUpdateXsInfo_xs() {
		return updateXsInfo_xs;
	}
	public void setUpdateXsInfo_xs(Xsb updateXsInfo_xs) {
		this.updateXsInfo_xs = updateXsInfo_xs;
	}
	public Xsb getXsInfoJsp_xs() {
		return xsInfoJsp_xs;
	}
	public void setXsInfoJsp_xs(Xsb xsInfoJsp_xs) {
		this.xsInfoJsp_xs = xsInfoJsp_xs;
	}
	public XsAction() {
		
		/*//1.首先从jsp页面中的session中获取loginAction中put的登录用户对象dluser
		Map<?, ?> session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		//2.然后利用dlUser的用户名变量，去查询学生对象，并将该对象赋值给xsInfo_
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		xsDao = (XsDao) context.getBean("xsDaoImp");
		//from spring 容器中获取zyDaoImp对象，然后查询专业对象
		zyDao = (ZyDao) context.getBean("zyDaoImp");
		//从spring容器中获取KcDaoImp对象，然后查询课程对象
		kcDao = (KcDao) context.getBean("kcDaoImp");
		//从spring容器中获取dlDaoImp对象，然后查询XX对象
		dlDao = (KcDao) context.getBean("dlDaoImp");*/
		
	}
	public String execute() throws Exception{
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		xsInfoJsp_xs = xsDao.getOneXs(user.getXh());
		return "success";
	}
	//新增updateXsinfo方法
	public String updateXsInfo() throws Exception{
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		updateXsInfoJsp_xs = xsDao.getOneXs(user.getXh());
		updateXsInfoJsp_zys = zyDao.getAll();
		return "success";
	}
	//执行updateXs.action 时调用updateXs方法
	public String updateXs() {
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		
		//根据xh值，从数据库中查询Xsb对象updateXs
		Xsb updateXs = xsDao.getOneXs(user.getXh());
		//然后将页面端updateActionXs 的各个新属性值赋值给updateXs 的各个属性
		updateXs.setXm(updateActionXs.getXm());
		updateXs.setXb(updateActionXs.getXb());
		updateXs.setCssj(updateActionXs.getCssj());
		updateXs.setZxf(updateActionXs.getZxf());
		updateXs.setBz(updateActionXs.getBz());
		updateXs.setZyb(updateActionXs.getZyb());
		xsDao.update(updateXs);
		return "success";
	}
	public String getAllKc() throws Exception{
		allKcJsp_kcs = kcDao.getAll();
		return "success";
	}
	//添加选课超连接功能
	
	public String selectKc() throws Exception{
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		
		Xsb xs = xsDao.getOneXs(user.getXh());
		//获得该学生已选的课程集合
		Set<Kcb> existedKcs = xs.getKcs();
		/**
		 * 迭代查找集合existedKcs 中是否存在待选修的课程编号selectActionkcb.getKch()
		 * 如果已经存在，则返回error 否则跳转selectKc_fail.jsp
		 * 如果不存在则将该项目加入到集合中
		 */
		Iterator<Kcb> it = existedKcs.iterator();
		while(it.hasNext())	{
			//获取当前有游标对象
			Kcb kcb = (Kcb) it.next();
			//先执行了trim
			if(kcb.getKch().trim().equals(selectActionKcb.getKch()))
				return "error";
		}
		//根据课程编号获得课程对象
		//注意1 ： 赋值前selectActionKcb 变量只有1个属性kch有值，其他所有属性(如kcm,kxxq,xs,xf) 都为空
		//attention2: 赋值完成后，selectActionKcb变量所有属性(kch,kcm,kxxq,xs,xf)都有值
		selectActionKcb = kcDao.getOneKc(selectActionKcb.getKch());
		//添加新选修的 课程集合到已选课程集合中
		existedKcs.add(selectActionKcb);
		//设置更新后的课程集合updateKcs
		xs.setKcs(existedKcs);
		//执行update更新操作，保存到数据库中
		xsDao.update(xs);
		return "success";
	}
	/*
	 * 个人选课情况方法
	 * 执行getXsKcs.action 时调用getXsKcs方法
	 * 
	 */
	public String getXsKcs() throws Exception
	{
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		Xsb xs  = xsDao.getOneXs(user.getXh());
		//获得该学生已选的课程集合
		xsKcsJsp_kcs = xs.getKcs();
		return "success";
	}
	//delete 课程表
	public String deleteKcb() throws Exception{
		Map session = ActionContext.getContext().getSession();
		user = (Dlb) session.get("dlUser");
		
		Xsb xs = xsDao.getOneXs(user.getXh());
		//获取该学生已选的课程集合
		Set updatedKcs = xs.getKcs();
		Iterator iter = updatedKcs.iterator();
		while(iter.hasNext()){
			Kcb kc = (Kcb) iter.next();
			if(kc.getKch().trim().equals(deleteActionKcb.getKch())){
				iter.remove();
			}
		}
		//设置更新后的课程集合updateKcs
		xs.setKcs(updatedKcs);
		//执行update更新操作，保存到数据库中
		xsDao.update(xs);
		return "success";
	}
}